package com.coderside.fragment.netWork;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

public class DBTool {

	protected static Connection con;
	protected static PreparedStatement pment;
	protected static ResultSet rs;

	protected static String driverclass;

	protected static Connection getConnection(String driverclass, String url, String username, String password) {

		try {
			Class.forName(driverclass);
			Connection connection = DriverManager.getConnection(url, username, password);
			return connection;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static void getDefaultConnection() {
//		String driverClass = "oracle.jdbc.driver.OracleDriver";
//		String url = "jdbc:oracle:thin:@10.0.24.89:1521:wctest";
//		String userName = "apps";
//		String password = "apps";

		String driverClass = "org.gjt.mm.mysql.Driver";
//		jdbc:mysql://10.0.24.20/coderside?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
		String url = "jdbc:mysql://10.0.24.89/coderside?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8";
		String userName = "root";
		String password = "123456";
		Connection connection = getConnection(driverClass, url, userName, password);
		setConnection(driverClass, connection);
	}

	protected static void setConnection(String driverclass, Connection connection) {
		setDriverclass(driverclass);
		DBTool.con = connection;
	}

	protected static void setDriverclass(String driverclass) {
		DBTool.driverclass = driverclass;
	}

	/**
	 * 关闭连接的方法
	 * 
	 * @param rs
	 * @param pstm
	 * @param con
	 */
	protected void closeConnection() {
		try {
			if (null != con)
				con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void insertArticle(ArticleBling article) throws SQLException {
		if(null==con)
		{
			getDefaultConnection();
		}
		String sql = "insert into article(`id`, `content`, `title`, `group`, `score`, `relativetime`, `url`, `localimg`)  VALUES(?,?,?,?,?,?,?,?)";
		pment = con.prepareStatement(sql);
		pment.setString(1, article.getId());
		pment.setString(2, article.getContent());
		pment.setString(3, article.getTitle());
		pment.setString(4, article.getGroup());
		pment.setString(5, article.getScore());
		pment.setString(6, article.getRelativetime());
		pment.setString(7, article.getUrl());
		pment.setString(8, article.getLocalImg());
		pment.execute();
	}

	public static void insertArticles(List<ArticleBling> articles) {
		for (Iterator<ArticleBling> iterator = articles.iterator(); iterator.hasNext();) {
			ArticleBling article = (ArticleBling) iterator.next();
			try {
				insertArticle(article);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
